<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是C语言基础——控制语句相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV01-04-C语言-控制语句">
<meta property="og:url" content="https://sumumm.github.io/post/d7b81674.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是C语言基础——控制语句相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125194507922.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125195352926.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125200142589.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125203408073.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125205217591.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125205922983.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125210700859.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125211932332.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220126111212925.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220126133428798.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220126134630921.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220126140539529.png">
<meta property="article:published_time" content="2022-01-13T08:29:55.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.984Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV01-C语言基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125194507922.png">


<link rel="canonical" href="https://sumumm.github.io/post/d7b81674.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/d7b81674.html","path":"post/d7b81674.html","title":"LV01-04-C语言-控制语句"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV01-04-C语言-控制语句 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E9%A1%BA%E5%BA%8F%E7%BB%93%E6%9E%84"><span class="nav-text">一、顺序结构</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E9%80%89%E6%8B%A9%E7%BB%93%E6%9E%84"><span class="nav-text">二、选择结构</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-if%E2%80%A6else%E2%80%A6"><span class="nav-text">1. if…else… </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1%E5%9F%BA%E6%9C%AC%E5%BD%A2%E5%BC%8F"><span class="nav-text">1.1基本形式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2%E5%B5%8C%E5%A5%97%E5%BD%A2%E5%BC%8F"><span class="nav-text">1.2嵌套形式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3%E9%98%B6%E6%A2%AF%E5%BD%A2%E5%BC%8F"><span class="nav-text">1.3阶梯形式</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-switch"><span class="nav-text">2. switch </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E5%9F%BA%E6%9C%AC%E5%BD%A2%E5%BC%8F"><span class="nav-text">2.1基本形式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2%E5%8F%AF%E4%BB%A5%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F%E5%98%9B%EF%BC%9F"><span class="nav-text">2.2可以定义变量嘛？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3%E5%A4%9A%E4%B8%AA%E5%80%BC%EF%BC%9F"><span class="nav-text">2.3多个值？</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E5%BE%AA%E7%8E%AF%E7%BB%93%E6%9E%84"><span class="nav-text">三、循环结构</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-while"><span class="nav-text">1. while </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1%E5%9F%BA%E6%9C%AC%E5%BD%A2%E5%BC%8F-1"><span class="nav-text">1.1基本形式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2%E5%AE%9E%E4%BE%8B"><span class="nav-text">1.2实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-do%E2%80%A6while"><span class="nav-text">2. do…while </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E5%9F%BA%E6%9C%AC%E5%BD%A2%E5%BC%8F-1"><span class="nav-text">2.1基本形式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.2实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-for"><span class="nav-text">3. for </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E5%9F%BA%E6%9C%AC%E5%BD%A2%E5%BC%8F"><span class="nav-text">3.1基本形式</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E8%BE%85%E5%8A%A9%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5"><span class="nav-text">四、辅助控制语句</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-return"><span class="nav-text">1. return </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1%E5%9F%BA%E6%9C%AC%E5%BD%A2%E5%BC%8F-2"><span class="nav-text">1.1基本形式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2%E5%9F%BA%E6%9C%AC%E5%BA%94%E7%94%A8"><span class="nav-text">1.2基本应用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-goto"><span class="nav-text">2. goto </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E5%9F%BA%E6%9C%AC%E5%BD%A2%E5%BC%8F-2"><span class="nav-text">2.1基本形式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2%E5%9F%BA%E6%9C%AC%E5%BA%94%E7%94%A8"><span class="nav-text">2.2基本应用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-break"><span class="nav-text">3. break </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E5%9F%BA%E6%9C%AC%E5%BD%A2%E5%BC%8F-1"><span class="nav-text">3.1基本形式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E5%9F%BA%E6%9C%AC%E5%BA%94%E7%94%A8"><span class="nav-text">3.2基本应用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-continue"><span class="nav-text">4. continue </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1%E5%9F%BA%E6%9C%AC%E5%BD%A2%E5%BC%8F"><span class="nav-text">4.1基本形式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2%E5%9F%BA%E6%9C%AC%E5%BA%94%E7%94%A8"><span class="nav-text">4.2基本应用</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/d7b81674.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV01-04-C语言-控制语句 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV01-04-C语言-控制语句
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-01-13 16:29:55" itemprop="dateCreated datePublished" datetime="2022-01-13T16:29:55+08:00">2022-01-13</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV01-C语言基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>4.3k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>16 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是C语言基础——控制语句相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、顺序结构"><a href="#一、顺序结构" class="headerlink" title="一、顺序结构"></a><font size=3>一、顺序结构</font></h1><p>先执行第一条语句，然后是第二条、第三条……一直到最后一条语句，这称为顺序结构。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125194507922.png" alt="image-20220125194507922" style="zoom:50%;" />

<h1 id="二、选择结构"><a href="#二、选择结构" class="headerlink" title="二、选择结构"></a><font size=3>二、选择结构</font></h1><h2 id="1-if…else…"><a href="#1-if…else…" class="headerlink" title="1. if…else… "></a><font size=3>1. if…else… </font></h2><h3 id="1-1基本形式"><a href="#1-1基本形式" class="headerlink" title="1.1基本形式"></a><font size=3>1.1基本形式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(表达式)</span><br><span class="line">&#123;</span><br><span class="line">    语句块<span class="number">1.</span>..;     <span class="comment">// 表达式为真，执行语句1</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">&#123;</span><br><span class="line">    语句块<span class="number">2.</span>..;    <span class="comment">// 表达式非真，执行语句2</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125195352926.png" alt="image-20220125195352926" style="zoom:50%;" />

<h3 id="1-2嵌套形式"><a href="#1-2嵌套形式" class="headerlink" title="1.2嵌套形式"></a><font size=3>1.2嵌套形式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(表达式)</span><br><span class="line"> 	 <span class="keyword">if</span>(表达式)    语句块<span class="number">1.</span>..;</span><br><span class="line">	 <span class="keyword">else</span> 	      语句块<span class="number">2.</span>..;</span><br><span class="line"><span class="keyword">else</span>	</span><br><span class="line">	 <span class="keyword">if</span>(表达式)    语句块<span class="number">3.</span>..;</span><br><span class="line">	 <span class="keyword">else</span>	      语句块<span class="number">4.</span>..;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="1-3阶梯形式"><a href="#1-3阶梯形式" class="headerlink" title="1.3阶梯形式"></a><font size=3>1.3阶梯形式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (表达式<span class="number">1</span>) 语句块<span class="number">1.</span>..;</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(表达式<span class="number">2</span>) 语句块<span class="number">2.</span>..;</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(表达式<span class="number">3</span>) 语句块<span class="number">3.</span>..;</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(表达式<span class="number">4</span>) 语句块<span class="number">4.</span>..;</span><br><span class="line">...</span><br><span class="line"><span class="keyword">else</span> 语句块n...;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125200142589.png" alt="image-20220125200142589" style="zoom:40%;" />

<p><strong>【注意】</strong></p>
<p>（1）语句块只有单条语句时， { } 可以省略，但是当有若干条语句时，必须用 {  } 括起来。</p>
<p>（2）对于表达式，一般情况下为逻辑表达式或关系表达式；也可以是任意类型(包括整型、实型、字符型、指针类型）</p>
<p>（3）要注意表达式中的判断条件原则是<strong>非0即为真</strong>。</p>
<p>（4） if 语句嵌套时， else 总是与它上面的<strong>最近的 if 配对</strong>，与书写格式无关。要实行强行的配对关系可以利用 { }  来实现。</p>
<h2 id="2-switch"><a href="#2-switch" class="headerlink" title="2. switch "></a><font size=3>2. switch </font></h2><h3 id="2-1基本形式"><a href="#2-1基本形式" class="headerlink" title="2.1基本形式"></a><font size=3>2.1基本形式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span>(表达式)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">case</span> 常量表达式<span class="number">1</span>:语句块<span class="number">1.</span>..;<span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> 常量表达式<span class="number">2</span>:语句块<span class="number">2.</span>..;<span class="keyword">break</span>;</span><br><span class="line">        ...</span><br><span class="line">    <span class="keyword">case</span> 常量表达式n:语句块n...;<span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:语句块n+<span class="number">1</span>;<span class="keyword">break</span>; <span class="comment">/* 放在最后时default后边的break可以省略，其他位置则不可省略 */</span>     </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125203408073.png" alt="image-20220125203408073" style="zoom:50%;" />

<p><strong>【注意】</strong></p>
<p>（1）每个常量表达式的值必须<strong>各不相同</strong>，否则将会出现矛盾。</p>
<p>（2）当表达式的值与 case 后面的常量表达式值（可以是<strong>整型、字符型或者枚举型</strong>）相等时，就执行此case后面的语句。 case 常量： 只起语句标号的作用。另外要注意<strong>多个 case 可以执行一组语句</strong>。 </p>
<p>（3） switch 中的表达式可以是<strong>整型、字符型或枚举型</strong>。</p>
<p>（4） break 语句用于强行跳出 switch ，一般每个 case 后面应有一个 break 语句， default 分支后的 break 可以省略。一般情况 default 分支都放在最后面，当然，也可以放在开头或者中间，但是此时的 break 就不可以省略了。</p>
<p>（5）语句块是可以使用花括号 { } 包裹起来的，但是大多数情况似乎都不使用花括号。</p>
<h3 id="2-2可以定义变量嘛？"><a href="#2-2可以定义变量嘛？" class="headerlink" title="2.2可以定义变量嘛？"></a><font size=3>2.2可以定义变量嘛？</font></h3><p>为什么会有这一部分笔记呢？因为踩坑了呗，先看一个实例吧：</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 主函数 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> n = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">switch</span>(n)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;1\n&quot;</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;2\n&quot;</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;default\n&quot;</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<p>然后我们编译上边的程序，这个时候是不会有任何问题的，但是要是我们想在分支定义一个局部变量怎么办？我们来试一下：</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 主函数 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> n = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">switch</span>(n)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;1\n&quot;</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">			<span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;2 a=%d\n&quot;</span>, a);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;default\n&quot;</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<p>这个时候我们再来编译一下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">hk@vm:~/6temp/test$ gcc test.c -Wall</span><br><span class="line">test.c: In function ‘main’:</span><br><span class="line">test.c:13:4: error: a label can only be part of a statement and a declaration is not a statement</span><br><span class="line">    int a = 10;</span><br><span class="line">    ^</span><br></pre></td></tr></table></figure>

<p>我们发现，居然报错了，原因上边其实已经提示了，在c语言中想要在switch语句的某一个分支中定义变量，我们需要加上为这分支加上花括号。将上边报错的分支修改为如下的形式（花括号也可将break包裹在内）：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;2 a=%d\n&quot;</span>, a);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">break</span>;</span><br></pre></td></tr></table></figure>

<h3 id="2-3多个值？"><a href="#2-3多个值？" class="headerlink" title="2.3多个值？"></a><font size=3>2.3多个值？</font></h3><p>有这样的情况怎么办，有0 1 2三个匹配对象执行相同的语句，switch也是支持的：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 主函数 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> n = <span class="number">2</span>;</span><br><span class="line">	<span class="keyword">switch</span> (n)</span><br><span class="line">	&#123;</span><br><span class="line">	<span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">	<span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;2\n&quot;</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">default</span>:</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;default\n&quot;</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样 n 是 1 或者 2 的时候都会执行分支2中的语句啦。</p>
<h1 id="三、循环结构"><a href="#三、循环结构" class="headerlink" title="三、循环结构"></a><font size=3>三、循环结构</font></h1><h2 id="1-while"><a href="#1-while" class="headerlink" title="1. while "></a><font size=3>1. while </font></h2><h3 id="1-1基本形式-1"><a href="#1-1基本形式-1" class="headerlink" title="1.1基本形式"></a><font size=3>1.1基本形式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span>(表达式)</span><br><span class="line">&#123;</span><br><span class="line">    语句块...;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>先计算“表达式”的值，当值为真（非0）时， 执行“语句块”；执行完“语句块”，再次计算表达式的值，如果为真，继续执行“语句块”……这个过程会一直重复，直到表达式的值为假（0），就退出循环，执行  while 整体 后面的代码。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125205217591.png" alt="image-20220125205217591" style="zoom:60%;" />

<h3 id="1-2实例"><a href="#1-2实例" class="headerlink" title="1.2实例"></a><font size=3>1.2实例</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdio.h&gt;</span></span><br><span class="line"></span><br><span class="line">int main(int argc, char *argv[])</span><br><span class="line">&#123;</span><br><span class="line">    int n = 5;</span><br><span class="line">    int count = 0;</span><br><span class="line">    printf(&quot;------cycle start------\n&quot;);</span><br><span class="line">    while(n &gt; 2)</span><br><span class="line">    &#123;</span><br><span class="line">        printf(&quot;Before statement execution: count=%d, n=%d\n&quot;, count, n);</span><br><span class="line">        n--;</span><br><span class="line">        count++;</span><br><span class="line">        printf(&quot;After statement execution: count=%d, n=%d\n&quot;, count, n);</span><br><span class="line">    &#125;</span><br><span class="line">    printf(&quot;------cycle end------\n&quot;);</span><br><span class="line">    printf(&quot;count = %d, n = %d \n&quot;, count, n);</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125205922983.png" alt="image-20220125205922983" style="zoom:50%;" />

<p>【注意】 while 循环是<strong>先判断</strong>表达式为真，<strong>然后才执行</strong>循环，若为假，则直接结束循环，不会再执行循环体内的语句块。</p>
<h2 id="2-do…while"><a href="#2-do…while" class="headerlink" title="2. do…while "></a><font size=3>2. do…while </font></h2><h3 id="2-1基本形式-1"><a href="#2-1基本形式-1" class="headerlink" title="2.1基本形式"></a><font size=3>2.1基本形式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">do</span>&#123;</span><br><span class="line">    语句块...;</span><br><span class="line">&#125;<span class="keyword">while</span>(表达式);</span><br></pre></td></tr></table></figure>

<p>先执行“语句块”，再计算“表达式”的值，当值为真（非0）时，继续执行“语句块”……这个过程会一直重复，直到表达式的值为假（0），就退出循环，执行  do…while  整体后面的代码。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125210700859.png" alt="image-20220125210700859" style="zoom:67%;" />

<h3 id="2-2实例"><a href="#2-2实例" class="headerlink" title="2.2实例"></a><font size=3>2.2实例</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdio.h&gt;</span></span><br><span class="line"></span><br><span class="line">int main(int argc, char *argv[])</span><br><span class="line">&#123;</span><br><span class="line">    int n = 5;</span><br><span class="line">    int count = 0;</span><br><span class="line">    printf(&quot;------cycle start------\n&quot;);</span><br><span class="line">    do&#123;</span><br><span class="line">        printf(&quot;Before statement execution: count=%d, n=%d\n&quot;, count, n);</span><br><span class="line">        n--;</span><br><span class="line">        count++;</span><br><span class="line">        printf(&quot;After statement execution: count=%d, n=%d\n&quot;, count, n);</span><br><span class="line">    &#125;while(n &gt; 5);</span><br><span class="line">    printf(&quot;------cycle end------\n&quot;);</span><br><span class="line">    printf(&quot;count = %d, n = %d \n&quot;, count, n);</span><br><span class="line"></span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220125211932332.png" alt="image-20220125211932332" style="zoom:50%;" />

<p>【注意】 do…while 循环是<strong>先执行一次循环，然后才判断</strong>表达式为真，然后才继续执行循环，若为假，则直接结束循环，不会再执行循环体内的语句块。所以当两个实例中的判断条件 n &gt; 2 变为 n &gt; 5 时 while 循环结构一次也不执行，而 do…while 循环结构会执行一次，这点需要注意。</p>
<h2 id="3-for"><a href="#3-for" class="headerlink" title="3. for "></a><font size=3>3. for </font></h2><h3 id="3-1基本形式"><a href="#3-1基本形式" class="headerlink" title="3.1基本形式"></a><font size=3>3.1基本形式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(表达式<span class="number">1</span>; 表达式<span class="number">2</span>; 表达式<span class="number">3</span>)</span><br><span class="line">&#123;</span><br><span class="line">    语句块...;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>执行过程：</p>
<p>(1) 先执行“表达式1”。</p>
<p>(2) 再执行“表达式2”，如果它的值为真（非0），则执行循环体，否则结束循环。</p>
<p>(3) 执行完循环体后再执行“表达式3”。</p>
<p>(4) 重复执行步骤 (2) 和 (3)，直到“表达式2”的值为假，就结束循环。</p>
<p>上面的步骤中，(2) 和 (3) 是一次循环，会重复执行，for 语句的主要作用就是不断执行步骤 (2) 和 (3)。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220126111212925.png" alt="image-20220126111212925" style="zoom:50%;" />



<p>【注意】</p>
<p>（1）“表达式1”<strong>仅在第一次循环时执行</strong>，以后都不会再执行，可以认为这是一个初始化语句。“表达式1”<strong>可省略,但循环之前应给循环变量赋值</strong>。</p>
<p>（2）表达式2”一般是一个<strong>关系表达式</strong>，决定了是否还要继续下次循环，称为“循环条件”。“表达式2”<strong>可省略,将陷入死循环</strong>。</p>
<p>（3）“表达式3”很多情况下是一个带有自增或自减操作的表达式，以<strong>使循环条件逐渐变得“不成立”</strong>。“表达式3”<strong>可省略,但在循环体中增加使循环变量值改变的语句</strong>。</p>
<h1 id="四、辅助控制语句"><a href="#四、辅助控制语句" class="headerlink" title="四、辅助控制语句"></a><font size=3>四、辅助控制语句</font></h1><h2 id="1-return"><a href="#1-return" class="headerlink" title="1. return "></a><font size=3>1. return </font></h2><h3 id="1-1基本形式-2"><a href="#1-1基本形式-2" class="headerlink" title="1.1基本形式"></a><font size=3>1.1基本形式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> (表达式); <span class="comment">/* 有没有 ( ) 都可以*/</span></span><br></pre></td></tr></table></figure>

<h3 id="1-2基本应用"><a href="#1-2基本应用" class="headerlink" title="1.2基本应用"></a><font size=3>1.2基本应用</font></h3><ul>
<li>返回函数的值</li>
</ul>
<p>当 return 应用在函数中时，可以用于返回一个值，如 return (10 + 9); </p>
<ul>
<li>结束程序</li>
</ul>
<p>当 return 应用在函数中时，不仅可以用于返回一个值，还可以直接结束该函数， return 语句执行完毕，它后边的所有语句不再执行，直接跳到主函数继续执行，若是用在主函数中，则遇到 return 直接结束主函数，整个程序结束，后边的语句不再执行。</p>
<p>【注意】  return  语句<strong>可以有多个</strong>，可以出现在函数体的<strong>任意位置</strong>，但是每次调用函数<strong>只能有一个  return  语句被执行</strong>，所以只有一个返回值（少数的编程语言支持多个返回值，例如 Go 语言）。</p>
<h2 id="2-goto"><a href="#2-goto" class="headerlink" title="2. goto "></a><font size=3>2. goto </font></h2><p> goto  语句是一种无条件流程跳转语句，通常  goto  语句与  if  语句结合使用，当满足一定条件时，程序流程跳转到指定标号处，接着往下执行。</p>
<h3 id="2-1基本形式-2"><a href="#2-1基本形式-2" class="headerlink" title="2.1基本形式"></a><font size=3>2.1基本形式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义一个语句标识 flag_name （flag_name只是一个名字）*/</span></span><br><span class="line">flag_name :      <span class="comment">/* 语句标识后的冒号不能省略 */</span></span><br><span class="line">	语句块...;</span><br><span class="line">	</span><br><span class="line"><span class="comment">/* goto 语句的调用 */</span></span><br><span class="line"><span class="keyword">goto</span> flag_name;  <span class="comment">/* 程序将从对应 flag_name 的代码处开始往下执行。 */</span></span><br></pre></td></tr></table></figure>

<p>【注意】 flag_name 后边的语句可以加 { } ，也可以不加。不过加上的话可读性会强一些。</p>
<h3 id="2-2基本应用"><a href="#2-2基本应用" class="headerlink" title="2.2基本应用"></a><font size=3>2.2基本应用</font></h3><ul>
<li>跳出循环</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> count = <span class="number">0</span>;</span><br><span class="line">    flag:</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;------goto statement execution------\n&quot;</span>);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;------cycle end------\n&quot;</span>);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;count = %d\n&quot;</span>, count);</span><br><span class="line">        <span class="keyword">if</span> (count == <span class="number">3</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;------while cycle start------\n&quot;</span>);</span><br><span class="line">    <span class="keyword">while</span>(<span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Before statement execution: count=%d\n&quot;</span>, count);</span><br><span class="line">        count++;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;After statement execution: count=%d\n&quot;</span>, count);</span><br><span class="line">        <span class="keyword">if</span>(count &gt; <span class="number">3</span>)</span><br><span class="line">            <span class="keyword">goto</span> flag;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220126133428798.png" alt="image-20220126133428798" style="zoom:50%;" />

<p> goto flag_name; 语句放在循环中用于跳出循环，用于结束循环时，此时 flag_name 处的代码有两种情况：</p>
<p>（1）第一种是放在循环上边，比循环先执行，这个时候， flag_name 处的程序会先执行一遍，所以，切记，这里边不要直接放 return 语句，否则程序直接结束了，有需要的话可以加一个判断，以保证程序可以正常运行。</p>
<p>（2）第二种是放在循环下边，这个时候，只有满足了跳转指令时，程序才会跳转到 flag_name 处执行。</p>
<ul>
<li>构成循环</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i = <span class="number">1</span>;</span><br><span class="line">    <span class="type">int</span> sum=<span class="number">0</span>;</span><br><span class="line">    flag: </span><br><span class="line">    	<span class="keyword">if</span>(i &lt;= <span class="number">5</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            sum = sum + i;</span><br><span class="line">            i++;</span><br><span class="line">            <span class="keyword">goto</span> flag;</span><br><span class="line">        &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,sum);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="3-break"><a href="#3-break" class="headerlink" title="3. break "></a><font size=3>3. break </font></h2><h3 id="3-1基本形式-1"><a href="#3-1基本形式-1" class="headerlink" title="3.1基本形式"></a><font size=3>3.1基本形式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">break</span>;</span><br></pre></td></tr></table></figure>

<p> break 语句通常用在循环语句和开关语句中。当 break 用于开关语句 switch 中时，可使程序跳出 switch 而执行 switch 以后的语句；如果没有 break 语句，则将成为一个死循环而无法退出。</p>
<p>当 break 语句用于 do-while、for、while 循环语句中时，可使程序终止循环而执行循环后面的语句，通常 break 语句总是与 if 语句联在一起，即满足条件时便跳出循环。</p>
<h3 id="3-2基本应用"><a href="#3-2基本应用" class="headerlink" title="3.2基本应用"></a><font size=3>3.2基本应用</font></h3><ul>
<li>跳出<strong>一层</strong>循环</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> n,i;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;自数1~100全部素数:\n&quot;</span>);</span><br><span class="line">    <span class="keyword">for</span>(n = <span class="number">2</span>; n &lt;= <span class="number">100</span>; n++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span>(i = <span class="number">2</span>; i &lt; n; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (n % i == <span class="number">0</span>)</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(i &gt;= n)</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d\t&quot;</span>,n);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220126134630921.png" alt="image-20220126134630921" style="zoom:67%;" />

<p><strong>【注意】</strong></p>
<p>（1） break 语句对 if-else 的条件语句不起作用；</p>
<p>（2）在多层循环中，一个 break 语句只向外跳一层。</p>
<h2 id="4-continue"><a href="#4-continue" class="headerlink" title="4. continue "></a><font size=3>4. continue </font></h2><h3 id="4-1基本形式"><a href="#4-1基本形式" class="headerlink" title="4.1基本形式"></a><font size=3>4.1基本形式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">continue</span>;</span><br></pre></td></tr></table></figure>

<p> continue 语句的作用是跳过循环体中剩余的语句而<strong>强行执行下一次循环</strong>。 continue 语句只用在 for、while、do-while 等循环体中，常与 if 条件语句一起使用，用来<strong>加速循环</strong>。</p>
<h3 id="4-2基本应用"><a href="#4-2基本应用" class="headerlink" title="4.2基本应用"></a><font size=3>4.2基本应用</font></h3><ul>
<li>直接跳过剩余语句执行下一次循环</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> n;</span><br><span class="line">    <span class="keyword">for</span>(n = <span class="number">1</span>; n &lt;= <span class="number">30</span>; n++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span>(n % <span class="number">2</span> == <span class="number">0</span>)</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d\t&quot;</span>, n);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-04-C%E8%AF%AD%E8%A8%80-%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/img/image-20220126140539529.png" alt="image-20220126140539529" style="zoom: 67%;" />


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/d7b81674.html">LV01-04-C语言-控制语句</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年01月13日 - 16:29</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/d7b81674.html" title="LV01-04-C语言-控制语句">https://sumumm.github.io/post/d7b81674.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV01-C语言基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/582937ae.html" rel="prev" title="LV01-05-C语言-数组与字符串">
                  <i class="fa fa-angle-left"></i> LV01-05-C语言-数组与字符串
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/b9a7171d.html" rel="next" title="LV01-03-C语言-输入与输出">
                  LV01-03-C语言-输入与输出 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
